<?php

namespace App\Http\Services;


use App\Http\Repositories\Daos\User\UserDao;
use App\Http\Requests\UserContext;
use App\Http\Traits\Errors;
use App\Models\Message;
use App\Models\Shop;
use App\Models\UserBenefit;
use App\Models\Users;
use App\Models\UserScoreRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;


class UserCenterService extends BaseService
{
    use Errors;

    protected $userDao;

    public function __construct(UserDao $userDao)
    {
        $this->userDao = $userDao;
    }

    public function userInfo(Request $request)
    {
        $userId = $request->userId;
        $field  = ['id', 'mobile', 'nickname', 'score', 'avatar', 'role_value', 'teacher_id'];
        return $this->userDao->getOne(['id' => $userId], [], $field);
    }

    public function updateUserInfo(UserContext $userContext)
    {
        $userId = $userContext->getId();
        $post   = request()->input();
        $user   = Users::where('id', $userId)->first();
        if (!empty($post['nickname'])) {
            $user->nickname = $post['nickname'];
        }
        if (!empty($post['real_name'])) {
            $user->real_name = $post['real_name'];
        }
        if (!empty($post['contact'])) {
            $user->contact = $post['contact'];
        }
        if (!empty($post['plot'])) {
            $user->plot = $post['plot'];
        }
        if (!empty($post['role_value'])) {
            $user->role_value = $post['role_value'];
        }
        if (!empty($post['avatar'])) {
            $user->avatar = $post['avatar'];
        }
        Log::info('更新用户' . $userId . '信息', $post);
        $user->save();
        return true;
    }

    public function messageList(UserContext $userContext)
    {
        $userId = $userContext->getId();
        $page   = request('page') ?? 1;
        $size   = request('size') ?? 10;
        $field  = ['*'];
        $offset = ($page * $size) - $size;
        return Message::where('user_id', $userId)->orderBy('is_read', 'asc')->orderBy('created_at', 'desc')->limit($size)->offset($offset)->get($field);
    }

    public function readMessage(UserContext $userContext)
    {
        $userId = $userContext->getId();
        $id     = \request('id') ?? 0;
        return Message::where('id', $id)->where('user_id', $userId)->update(['is_read' => 2]);
    }

    public function createUserBenefit(UserContext $userContext)
    {
        $userId    = $userContext->getId();
        $benefitId = \request('benefit_id') ?? 0;
//        $remark    = \request('remark') ?? 0;
        if (empty($benefitId)) $this->showError('请选择报名的公益');
//        if (empty($remark)) $this->showError('请填写报名备注');
//        if (mb_strlen($remark) > 500) $this->showError('备注不能超过500字');
        $info = UserBenefit::where('user_id', $userId)->where('benefit_id', $benefitId)->first();
        if (!empty($info)&&$info['approve_status']==1) $this->showError('您的报名在审核中');
        if (!empty($info)&&$info['approve_status']==2) $this->showError('您已经报名此公益了');
        if (!empty($info)&&$info['approve_status']==3) $this->showError('您的报名被拒绝');
        UserBenefit::create(['user_id' => $userId, 'benefit_id' => $benefitId]);
        return true;
    }

    public function userBenefitList(UserContext $userContext)
    {
        $userId = $userContext->getId();
        $page   = request('page') ?? 1;
        $size   = request('size') ?? 4;
        $field  = ['*'];
        $offset = ($page * $size) - $size;
        return UserBenefit::where('user_id', $userId)->where('approve_status',2)->with(["benefitInfo" => function ($q) {
            $q->select(['id', 'title', 'pics', 'campus_id'])->with(["campusInfo" => function ($query) {
                $query->select(['id', 'name', 'logo']);
            }]);
        }])->orderBy('created_at', 'desc')->limit($size)->offset($offset)->get($field);
    }

    public function userCompanyInfo(UserContext $userContext)
    {
        $userId = $userContext->getId();
        $info   = Shop::where('user_id', $userId)->first();
        if (empty($info) || $info['status'] == 0) {
            return ['status' => 0];
        }
        return ['status' => 1];
    }

    public function scoreList(UserContext $userContext)
    {
        $userId = $userContext->getId();
        $page   = request('page') ?? 1;
        $size   = request('size') ?? 10;
        $field  = ['*'];
        $offset = ($page * $size) - $size;
        return UserScoreRecord::where('user_id', $userId)->orderBy('created_at', 'desc')->limit($size)->offset($offset)->get($field);
    }
}
